package com.metel.mservice.common.bean;

import com.thoughtworks.xstream.annotations.XStreamAlias;

import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

@XStreamAlias("country")
public class Country {
	
	@XStreamAlias("iso2")
	private String iso2;
	
	@XStreamAlias("iso3")
	private String iso3;

	@XStreamAlias("name")
	private String name;
	
	@XStreamAlias("engname")
	private String engname;
	
	@XStreamAlias("mcc")
	private String mcc;

	@XStreamAlias("intlcode")
	private String intlcode;
	
	public Country() { }
	
	@Override
	public String toString() {
		JsonConfig jsonCfg = new JsonConfig();  
		jsonCfg.setExcludes( new String[]{ "mcc", "imsi" } );  
		return JSONObject.fromObject(JSONSerializer.toJSON(this, jsonCfg)).toString();
	}

	public String getIso2() {
		return iso2;
	}

	public void setIso2(String iso2) {
		this.iso2 = iso2;
	}

	public String getIso3() {
		return iso3;
	}

	public void setIso3(String iso3) {
		this.iso3 = iso3;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEngname() {
		return engname;
	}

	public void setEngname(String engname) {
		this.engname = engname;
	}

	public String getMcc() {
		return mcc;
	}

	public void setMcc(String mcc) {
		this.mcc = mcc;
	}
	
	public String getImsi() {
		// [12.03.07] eunhee97.lim 
		// - 국내오픈 시 한국사용자는 일본으로 임시 교체 
		if("450".equals(mcc) || "460".equals(mcc) || "461".equals(mcc)) {
			mcc = "440";
		}
		return mcc + "0000000000000";
	}
	
	public String getIntlcode() {
		return intlcode;
	}

	public void setIntlcode(String intlcode) {
		this.intlcode = intlcode;
	}
	
	public String genMsisdn(String phoneNumer) {
		if(phoneNumer == null) {
			return null;
		}
		phoneNumer = phoneNumer.replaceAll("[^\\d]", "");
		if(phoneNumer.startsWith(getIntlcode())) {
			return phoneNumer;
		}
		if(phoneNumer.charAt(0) == '0') {
			phoneNumer = phoneNumer.substring(1);
		}
		if(Countries.ISO2_Argentine.equals(getIso2()) && phoneNumer.charAt(0) != '9') { //special cases
			phoneNumer = "9" + phoneNumer;
		}
		return getIntlcode() + phoneNumer;
	}

}
